package com.artpri.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.artpri.model.DeliverAdd;
import com.artpri.service.DeliverAddService;

/**
 * 收货地址
 * @author ZhangSi
 *
 */
public class DeliverAddServiceImpl extends BaseService implements DeliverAddService {

	
	@Override
	@Transactional
	public void addDeliver(DeliverAdd address, String province, String city, String town) {
		String region = province + "," + city + "," + town;
		address.setDaRegion(region);
		address.setIfdefault(0);
		if (address.getAddressName().trim().equals("")) {
			address.setAddressName(address.getDaName() + " " + province);
		}
		getDeliverAddDao().saveOrUpdate(address);
	}

	@Override
	@Transactional(readOnly=true)
	public List<DeliverAdd> listDeliver(Integer userNum) {
		return getDeliverAddDao().getDeliverAddByUser(userNum);
	}

	@Override
	@Transactional
	public void deleteDeliver(Integer id) {
		getDeliverAddDao().delete(DeliverAdd.class, id);
	}
	
	/*@Override
	@Transactional
	public void updateDeliver(DeliverAdd address) {
		getDeliverAddDao().update(address);
	}
*/
	@Override
	@Transactional
	public void setDefaultAdd(Integer userNum, Integer id) {
		DeliverAdd nadd = getDeliverAddDao().get(DeliverAdd.class, id);
		DeliverAdd oadd = getDeliverAddDao().getDefaultAdd(userNum);
		if (oadd != null) {
			oadd.setIfdefault(0);
		}
		nadd.setIfdefault(1);
	}

	@Override
	@Transactional(readOnly=true)
	public DeliverAdd get(Integer id) {
		return getDeliverAddDao().get(DeliverAdd.class, id);
	}

	@Override
	@Transactional(readOnly=true)
	public int getAddressCount(Integer userNum) {
		return getDeliverAddDao().getAddressCount(userNum);
	}
}
